<?php
require_once('../../core.inc.php');
require_once('Model/SiteManage.php');
require_once('View/Smarty.php');
require_once('Model/DB.php');
require_once('Model/Validate.php');
require_once('Plugin/Category.php');
require_once('Plugin/BannerConfig.php');

$sys = new Model_SiteManage();
$sys->checkLogin();
$params = $_REQUEST;
$smarty = new View_Smarty($params);
$banner = new Model_bannerConfig();
$smarty->assign('banner', $banner);


if (isset($params['do']) && function_exists('exec_' . $params['do'])){
   $params['op'] = call_user_func('exec_' . $params['do'], &$params, &$smarty, &$banner);
}
if (isset($params['op']) && function_exists('op_' . $params['op'])){
    call_user_func('op_' . $params['op'], $params, &$smarty, &$banner);
}
else{
    call_user_func('op_edit', $params, &$smarty, &$banner);
}


function op_edit($params, &$smarty, &$banner)
{
    $config = __get_config('plugin.ini');

    if (!isset($params['forms'])){
        $db = new Model_DB();
	$sql = sprintf( "SELECT * FROM M_BANNER WHERE site = %u ORDER BY order_num", $_SESSION['DASH_BOARD']['SITE_ID'] );
	$db->query( $sql );
	$i = 0;
	while( $row = $db->fetch() ){
		$forms['banner'][$i] = $row['name'];
		$forms['status'][$i] = $row['status'];
		$forms['array_id'][$i] = $row['id'];
		$i++;
	}
	
	if( $i == 0 ){
		$forms['banner'][0] = '';
		$forms['status'][0] = '1';
		$forms['array_id'][0] = '';
	}
    }
    
    $smarty->assign('status', $banner->getStatusMap());
    $smarty->assign('phase', 'input');
    $smarty->assign('forms', $forms);
    $smarty->display('banner/banner_config.html');
}

function exec_validate_edit_form(&$params, &$smarty, &$banner)
{
    $err = array();
    if ($banner->validate($params['forms'], $err)){
        return exec_edit_form($params, $smarty, $banner);
    }
    $validate = new Model_Validate();
    $smarty->assign('validate', $validate);
    $smarty->assign('err', $err);
    return 'edit';
}
function exec_edit_form(&$params, &$smarty, &$banner)
{
    $forms = $params['forms'];

    $db = new Model_DB();
    $order_num = 0;
    foreach( $forms['banner'] as $k => $v )
    {
        $v = trim( $v );

        if( isset( $forms['array_id'][$k] ) && $forms['array_id'][$k] != '' ){

            if( $v != '' ){
                // 更新
                $data = array(
                    'name' => $db->escape( $v ),
                    'status' => $forms['status'][$k],
                    'order_num' => $order_num
                    );
                $banner->update($forms['array_id'][$k], $data);
            }else{
                // 削除
                $banner->delete($forms['array_id'][$k]);
            }
        }else{
            // 新規登録
            $data = array(
                'site' => $_SESSION['DASH_BOARD']['SITE_ID'],
                'name' => $db->escape( $v ),
                'status' => $forms['status'][$k],
                'order_num' => $order_num
                );
                
            $banner->insert($data);
        }
        $order_num++;
    }
	
    header('Location: '. $_SERVER['SCRIPT_NAME'] . '?op=edit_complete');
    exit();
}
function op_edit_complete($params, &$smarty, &$banner)
{
    $smarty->assign('phase', 'complete');
    $smarty->assign('message', '更新が完了しました');
     $smarty->display('banner/banner_config.html');
}
?>